package com.example.project22_editingmediaplayer;

import java.util.ArrayList;

import com.example.minimusicdemo.Main_play;
import com.example.minimusicdemo.SongUtility;
import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowSong;
import com.example.project22_editingmediaplayer.objects.ArtisObject;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;
import com.example.provider.SamsungProvider;
import com.example.provider.TblSong;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class LayoutRecent extends Fragment {
	Context myContext;
	ListView listview_recent;
	ArrayList<SongObject> listSongrecent;
	RowSong rowAdapter;


	public static Fragment newInstance(Context context) {
		LayoutRecent f = new LayoutRecent();

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_recent,
				null);
		myContext = container.getContext();
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		listview_recent = (ListView) getActivity().findViewById(
				R.id.list_recent);
		listSongrecent = new ArrayList<SongObject>();
		rowAdapter = new RowSong(listSongrecent, myContext);
		listview_recent.setAdapter(rowAdapter);
//		listview_recent.setOnItemClickListener(onItemClick);
		new LoadRecentMusic().execute();
		registerForContextMenu(listview_recent);
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		// menu.setHeaderTitle("favorite");
		menu.add(0, v.getId(), 0, "Play All");
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		if (item.getTitle() == "Play All") {

			SongResource.listSong = rowAdapter.getAllData();

			MainActivity.mService.PlaySong(0);

			MainActivity.mService.showNotification(0,
					SongResource.listSong.get(0));
			MainActivity.btnPlay
					.setImageResource(R.drawable.apollo_holo_light_pause);
			MainActivity.namesong.setText(SongResource.listSong.get(0)
					.get_name());
			MainActivity.singer.setText(SongResource.listSong.get(0)
					.get_artist());
			MainActivity.mService.flag_first = false;
			Intent intent = new Intent(getActivity(), Main_play.class);
			startActivity(intent);
		} else {
			return false;
		}
		return true;

	}

	// play this item song
//	OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {
//
//		@Override
//		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
//				long arg3) {
//			// TODO Auto-generated method stub
//			SongResource.listSong.clear();
//			SongResource.listSong.add((SongObject) rowAdapter.getItem(position));
//			MainActivity.mService.PlaySong(0);
//			MainActivity.mService.showNotification(0,
//					SongResource.listSong.get(0));
//			MainActivity.btnPlay
//					.setImageResource(R.drawable.apollo_holo_light_pause);
//			MainActivity.namesong.setText(SongResource.listSong.get(0)
//					.get_name());
//			MainActivity.singer.setText(SongResource.listSong.get(0)
//					.get_artist());
//			MainActivity.mService.flag_first = false;
//			Intent intent = new Intent(getActivity(), Main_play.class);
//			startActivity(intent);
//		}
//	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	// load song data on recent listview
	private class LoadRecentMusic extends
			AsyncTask<Void, Void, ArrayList<SongObject>> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			super.onPreExecute();
		}

		@Override
		protected ArrayList<SongObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ArrayList<SongObject> arrayList = new ArrayList<SongObject>();
			AudioUltility audioUltility = new AudioUltility(myContext);
			if (SongResource.sharePrefList != null) {
				for (int i = 0; i < SongResource.sharePrefList.size(); i++) {
					arrayList.add(audioUltility.getOneSongById(SongResource.sharePrefList
							.get(i)));

				}
				return arrayList;
			} else
				return null;
		}

		@Override
		protected void onPostExecute(ArrayList<SongObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			rowAdapter.resetData(result);

		}

	}
}